package it.valeandre.omino.fragment;

import it.valeandre.omino.R;
import it.valeandre.omino.adapter.OminoPagerAdapter;
import it.valeandre.omino.utils.Utils;
import android.app.Activity;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class OminoPagerFragment extends UpgradableFragment {

	public static final String TAG = "OminoPagerFragment";

	private final int TAB_TRANSITION_DURATION = 200;
	private View mContentView;
	private ViewPager mPager;
	private TextView mTmpScoresLabel;
	private TextView mScoresLabel;
	private TextView mRankingLabel;
	private OminoPagerAdapter mAdapter;

	private LinearLayout mDateContainer;
	private TextView mDate;
	private View mDivider;

	private int mOldPosition;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mContentView = inflater.inflate(R.layout.fragment_omino_pager, container, false);
		mPager = (ViewPager) mContentView.findViewById(R.id.omino_pager_gallery);
		mTmpScoresLabel = (TextView) mContentView.findViewById(R.id.selector_tmp_scores_label);
		mScoresLabel = (TextView) mContentView.findViewById(R.id.selector_scores_label);
		mRankingLabel = (TextView) mContentView.findViewById(R.id.selector_ranking_label);

		mDateContainer = (LinearLayout) mContentView.findViewById(R.id.omino_pager_date_container);
		mDate = (TextView) mContentView.findViewById(R.id.omino_pager_date);
		mDivider = (View) mContentView.findViewById(R.id.omino_pager_date_second_divider);
		updateDate(Utils.readRankingLastDate(getActivity()));
		// mTmpScoresLabel.setSelected(false);
		// mScoresLabel.setSelected(true);
		// mRankingLabel.setSelected(false);

		mAdapter = new OminoPagerAdapter(getActivity(), this);
		mPager.setAdapter(mAdapter);
		mPager.setOffscreenPageLimit(2);

		// final TransitionDrawable scoresTmpTransitionDrawable =
		// (TransitionDrawable) mTmpScoresLabel.getBackground();
		// final TransitionDrawable scoresTransitionDrawable =
		// (TransitionDrawable) mScoresLabel.getBackground();
		// final TransitionDrawable rankingTransitionDrawable =
		// (TransitionDrawable) mRankingLabel.getBackground();

		// scoresTransitionDrawable.startTransition(TAB_TRANSITION_DURATION);
		// setTabSelected(mScoresLabel, true);

		mPager.setOnPageChangeListener(new OnPageChangeListener() {

			public void onPageSelected(int page) {
				switch (page) {
				case 0:
					setTabSelected(mTmpScoresLabel, true);
					setTabSelected(mScoresLabel, false);
					setTabSelected(mRankingLabel, false);
//                    setDateContainerVisible(false);
//					mDivider.setVisibility(View.GONE);
					mAdapter.setCurrentPage(OminoPagerAdapter.TMP_RESULTS_PAGE);
					break;
				case 1:
					setTabSelected(mTmpScoresLabel, false);
					setTabSelected(mScoresLabel, true);
					setTabSelected(mRankingLabel, false);
//					setDateContainerVisible(false);
//					mDivider.setVisibility(View.GONE);
					mAdapter.setCurrentPage(OminoPagerAdapter.RESULTS_PAGE);
					break;
				case 2:
					setTabSelected(mTmpScoresLabel, false);
					setTabSelected(mScoresLabel, false);
					setTabSelected(mRankingLabel, true);
//					setDateContainerVisible(true);
//					mDivider.setVisibility(View.VISIBLE);
					mAdapter.setCurrentPage(OminoPagerAdapter.RANKING_PAGE);
					break;

				default:
					break;
				}
			}

			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			public void onPageScrollStateChanged(int arg0) {

			}
		});

		mTmpScoresLabel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				mPager.setCurrentItem(0, true);

			}
		});

		mScoresLabel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				mPager.setCurrentItem(1, true);
			}
		});

		mRankingLabel.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				mPager.setCurrentItem(2, true);

			}
		});
		return mContentView;
	}

    private void setDateContainerVisible(final boolean visible) {
        mHandler.postDelayed(new Runnable() {
            public void run() {
                if(visible) {
                    mDateContainer.setVisibility(View.VISIBLE);
                } else {
                    mDateContainer.setVisibility(View.GONE);
                }

            }
        },2000);
    }
	
	private void setTabSelected(TextView tab, boolean selected) {
		final TransitionDrawable tabTransition = (TransitionDrawable) tab.getBackground();
		boolean wasSelected = tab.isSelected();
		if (selected) {
			if (!wasSelected) {
				tabTransition.startTransition(TAB_TRANSITION_DURATION);
			}
		} else {
			if (wasSelected) {
				tabTransition.reverseTransition(TAB_TRANSITION_DURATION);
			}
		}
		tab.setSelected(selected);
	}

	@Override
	public void onDestroyView() {
		if(mPager != null) {
			mOldPosition = mPager.getCurrentItem();
		} else {
			mOldPosition = 0;
		}
		
		super.onDestroyView();
	}

	@Override
	public void onResume() {
		super.onResume();
		
		mPager.setCurrentItem(mOldPosition);
		switch (mPager.getCurrentItem()) {
		case 0:
			setTabSelected(mTmpScoresLabel, true);
			setTabSelected(mScoresLabel, false);
			setTabSelected(mRankingLabel, false);
			break;

		case 1:
			setTabSelected(mTmpScoresLabel, false);
			setTabSelected(mScoresLabel, true);
			setTabSelected(mRankingLabel, false);
			break;

		case 2:
			setTabSelected(mTmpScoresLabel, false);
			setTabSelected(mScoresLabel, false);
			setTabSelected(mRankingLabel, true);
			break;

		default:
			break;
		}
		
		getInterFragmentInterface().setCurrentFragment(this);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		getInterFragmentInterface().expandActionBar();

	}

	@Override
	public void refresh() {

		mAdapter.refreshCurrentPage();
		updateDate(Utils.readRankingLastDate(getActivity()));

	}

	public void updateDate(final String date) {
		/*Utils.log("i", "Updating date: " + date);
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (mDate != null) {
                    mDate.setText(date);
                }
            }
        });*/
	}

	@Override
	public void refreshAll() {
		mAdapter.refreshAll();
		updateDate(Utils.readRankingLastDate(getActivity()));
		
	}

}
